<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <style>
        body {
            margin: 0;
        }

        ul {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .map {
            width: 500px;
            height: 400px;
            background-color: skyblue;
            margin: 50px auto;
        }

        .map ul {
            height: 100px;
        }

        .map ul li {
            width: 98px;
            height: 98px;
            border: 1px solid #ccc;
            float: left;
        }

        .map ul li.active {
            background-color: red;
        }

        .handle {
            width: 300px;
            height: 300px;
            margin: 0 auto;
        }

        .handle ul {
            height: 100px;
        }

        .handle ul li {
            width: 98px;
            height: 98px;
            border: 1px solid #ccc;
            float: left;
            text-align: center;
            line-height: 98px;
            font-size: 12px;
            color: #666;
            cursor: pointer;
        }

        .handle ul li:hover {
            background-color: #ddd;
        }

        .m_l_100 {
            margin-left: 100px;
        }
    </style>
</head>

<body>

    <div class="map">
        <!-- ul*4>li*5 -->
        <!-- 创建列表 -->
    </div>
    <div class="handle">
        <ul>
            <li id="up" class="m_l_100">上</li>
        </ul>
        <ul>
            <li id="left">左</li>
            <li id="right" class="m_l_100">右</li>
        </ul>
        <ul>
            <li id="down" class="m_l_100">下</li>
        </ul>
    </div>

    <script>

        // 需求：
        // 1、利用JS在页面动态创建ul>li标签，实现 4行5列排版（如图）
        // 2、利用数组合理记录所有创建的li标签 
        // 3、给 "上"，"下" ,"左","右"四个按钮绑定 点击事件
        // 4、通过点击按钮，根据方向切换元素的背景色
        var map = document.querySelector(".map");
        var handle_li = document.querySelectorAll(".handle li");
        var index = 0;

        for (var i = 0; i < 4; i++) {
            var ul = document.createElement("ul");
            map.appendChild(ul);
            for (var j = 0; j < 5; j++) {
                var li = document.createElement("li");
                ul.appendChild(li);
            }
        }

        var li_arr = document.querySelectorAll(".map li");
        li_arr[0].className = "active"

        handle_li[0].onclick = function () {
            li_arr[index].className = "";
            if (index < 5) {
                index += 15 ;
            } else {
                index -= 5;
            }
            li_arr[index].className = "active";
        }

        handle_li[1].onclick = function () {
            li_arr[index].className = "";
            if (index % 5 == 0) {
                index += 4;
            } else {
                index--;
            }
            li_arr[index].className = "active";
        }

        handle_li[2].onclick = function () {
            li_arr[index].className = "";
            if (index % 5 == 4) {
                index -= 4;
            } else {
                index++;
            }
            li_arr[index].className = "active";
        }

        handle_li[3].onclick = function () {
            li_arr[index].className = "";
            if (index > 14) {
                index -= 15;
            } else {
                index += 5;
            }
            li_arr[index].className = "active";
        }


    </script>
</body>

</html>